﻿Imports System.Globalization
Imports System.Drawing.Drawing2D

Public Class UcDispoEmploye

    Public Disponibilite As New List(Of DispoEmp)
    Private Loaded As Boolean = False
    Private bmpPrincipal As Bitmap = New Bitmap(Me.Width, Me.Height)

    Private Sub UcDispoEmploye_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bmpPrincipal = New Bitmap(Me.Width, Me.Height)
        Loaded = True
    End Sub
    Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)

    End Sub


    Public Sub RefreshDispo()
        Dessiner()
    End Sub

    Private Sub Dessiner()
        If bmpPrincipal Is Nothing Then
            Return
        End If
        Dim LargeurColonne = (bmpPrincipal.Width - 60) / 7
        Dim Semaine As Date = Date.Now
        Dim headerHeight As Integer = Me.Font.Height + 10
        Dim rect As New RectangleF(0, 0, Me.Width, headerHeight)
        If Disponibilite Is Nothing Then
            Disponibilite = New List(Of DispoEmp)
        End If
        Dim formatTexte As New StringFormat()
        formatTexte.Alignment = StringAlignment.Center
        formatTexte.LineAlignment = StringAlignment.Center
        formatTexte.Trimming = StringTrimming.Character
        Using g As Graphics = Graphics.FromImage(bmpPrincipal)
            g.Clear(Color.White)

            'Dessin des heures
            Dim sh As Integer = g.MeasureString("00:00", Me.Font).Height
            Dim sw As Integer = g.MeasureString("00:00", Me.Font).Width
            g.DrawString("00:00", Me.Font, Brushes.Black, New RectangleF(0, headerHeight + 1, 60, sh + 1), formatTexte)

            For i = 1 To 11
                g.DrawLine(SystemPens.ControlLight, New PointF(0, ((Me.Height - headerHeight - sh) / 12) * i + headerHeight + sh / 2), New PointF(Me.Width, ((Me.Height - headerHeight - sh) / 12) * i + headerHeight + sh / 2))
                g.FillRectangle(Brushes.White, New RectangleF((60 - sw) / 2, ((Me.Height - headerHeight - sh) / 12) * i + headerHeight, sw, sh))
                g.DrawString((i * 2).ToString("00") + ":00", Me.Font, Brushes.Black, New RectangleF(0, ((Me.Height - headerHeight - sh) / 12) * i + headerHeight, 60, sh + 1), formatTexte)
            Next

            g.DrawString("24:00", Me.Font, Brushes.Black, New RectangleF(0, Me.Height - sh - 1, 60, sh), formatTexte)

            'Dessin des jours
            Using br As New LinearGradientBrush(rect, Color.White, Color.FromArgb(230, 230, 240), LinearGradientMode.Vertical)
                g.FillRectangle(br, rect)
                g.DrawRectangle(New Pen(Color.FromArgb(158, 182, 206)), 0, 0, rect.Width - 1, rect.Height)
            End Using
            For i = 0 To 6
                rect = New RectangleF(LargeurColonne * i + 60, 5, LargeurColonne, headerHeight - 10)

                Dim texte As String = StrConv(Semaine.AddDays(-Semaine.DayOfWeek + i).ToString("dddd", CultureInfo.GetCultureInfo("fr-CA")), VbStrConv.ProperCase)
                

                g.DrawString(texte, Me.Font, Brushes.Black, rect, formatTexte)
                g.DrawLine(New Pen(Color.FromArgb(158, 182, 206)), New PointF(LargeurColonne * (i) + 60, 0), New PointF(LargeurColonne * (i) + 60, Me.Height))

            Next
            'Dessin des dispo
            If Loaded And Not Disponibilite Is Nothing Then
                For Each dispo As DispoEmp In Disponibilite
                    Dim starty As Single = ((Me.Height - headerHeight) / 1440) * dispo.HeureDebut.TimeOfDay.TotalMinutes
                    Dim endy As Single = ((Me.Height - headerHeight) / 1440) * dispo.HeureFin.TimeOfDay.TotalMinutes
                    'rect = New RectangleF(40, 40, 50, 50)
                    rect = New RectangleF(dispo.Jour * LargeurColonne + 2 + 60, headerHeight + starty, LargeurColonne - 4, endy - starty)
                    g.FillRectangle(New SolidBrush(Color.FromArgb(0, 214, 124)), rect)
                    g.DrawRectangle(New Pen(Color.FromArgb(0, 158, 96)), rect.X, rect.Y, rect.Width, rect.Height)
                Next
            End If

        End Using
        Using g As Graphics = Me.CreateGraphics()
            g.DrawImageUnscaled(bmpPrincipal, 0, 0)
        End Using
    End Sub

    Private Sub UcDispoEmploye_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        e.Graphics.DrawImageUnscaled(bmpPrincipal, 0, 0)
    End Sub

    Private Sub UcDispoEmploye_Resize(sender As System.Object, e As System.EventArgs) Handles MyBase.Resize
        bmpPrincipal.Dispose()
        bmpPrincipal = New Bitmap(Me.Width, Me.Height)
        Dessiner()
    End Sub

End Class
